package com.ebuy.shopping.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebuy.shopping.model.User;
import com.ebuy.shopping.service.IUserService;

@Controller
@RequestMapping("/user")
/**
 * 
 * @ClassName: UserController 
 * @Description: TODO 
 * @author wenzhao 
 * @date 2017年8月10日 上午10:23:08 
 *
 */
public class UserController
{
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "{user_id}/show", method = RequestMethod.GET)
    public String user(@PathVariable
    String user_id, Model model) throws Exception
    {
        User user = userService.load(user_id);
        model.addAttribute("user", user);
        return "user/show";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) throws Exception
    {
        List<User> users = userService.getUsers();
        model.addAttribute("users", users);

        return "user/user_list";

    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() throws Exception
    {

        return "user/user_add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(User user) throws Exception
    {
        int flag = userService.add(user);
        if (flag > 0)
        {

        }
        else
        {

        }
        return "redirect:/user/list";
    }

    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String goUpdate(@PathVariable
    String id, Model model) throws Exception
    {
        User user = userService.load(id);
        model.addAttribute("user", user);
        return "user/user_update";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(User user) throws Exception
    {

        user.setStatus(1);
        int flag = userService.update(user);
        if (flag > 0)
        {

        }
        else
        {

        }
        return "redirect:/user/list";
    }

    @RequestMapping(value = "{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable
    String id) throws Exception
    {
        userService.deleteByPrimaryKey(id);
        return "redirect:/user/list";
    }

}
